package com.xyy.saas.payment.payment.core.service;

import com.xyy.saas.payment.dao.model.RiskControlPayWhitelist;
import com.xyy.saas.payment.dao.model.RiskControlPayWhitelistQueryParam;
import com.xyy.saas.payment.service.RiskControlPayWhitelistService;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class RiskControlService {
    @Autowired
    private RiskControlPayWhitelistService riskControlPayWhitelistService;


    /**
     * 判断是否风控降级
     */
    public boolean isRiskControl(String businessType, String payerId, Byte type) {
        return isRiskControl(businessType, payerId, type, null, null, null);
    }

    /**
     * 判断是否风控降级
     */
    public boolean isRiskControl(String businessType, String payerId, Byte type, String payChannel, String payMode, String payScene) {
        log.info("RiskControlService#isRiskControl, businessType:{}, payerId:{}, type:{}, payChannel:{}, payMode:{}, payScene:{}",
                businessType, payerId, type, payChannel, payMode, payScene);

        if(StringUtils.isEmpty(businessType) || StringUtils.isEmpty(payerId) || type == null) {
            return false;
        }

        try {
            Date now = new Date();
            RiskControlPayWhitelistQueryParam param = new RiskControlPayWhitelistQueryParam();
            param.setPayerId(payerId);
            param.setType(type);
            param.setBusinessType(businessType);
            List<RiskControlPayWhitelist> payWhitelists = riskControlPayWhitelistService.pageListByParam(param);

            for(RiskControlPayWhitelist r : payWhitelists) {
                Date startTime = r.getStartTime();
                Date endTime = r.getEndTime();

                if(startTime.before(now) && endTime.after(now)) {
                    // 有效期内
                    String channelRule = r.getChannelRule();
                    List<String> rules = JSONUtils.parse2List(channelRule, String.class);
                    // 匹配
                    if(!CollectionUtils.isEmpty(rules)
                            && !StringUtils.isEmpty(payChannel)
                            && !StringUtils.isEmpty(payMode)
                            && !StringUtils.isEmpty(payScene))
                    {
                        for (String item : rules) {
                            Map<String, String> rule = JSONUtils.parse2Map(item, String.class, String.class);
                            if(!CollectionUtils.isEmpty(rule) &&
                                    //Objects.equals(payChannel, rule.get("channel")) &&
                                    payChannel.contains(rule.get("channel")) &&
                                    payMode.equals(rule.get("payMode")) &&
                                    payScene.equals(rule.get("payScene")))
                            {
                                return true;
                            }
                        }

                        return false;
                    }

                    return true;
                }
            }

            return false;
        }
        catch(Exception e) {
            log.error("RiskControlPayWhitelistServiceImpl#isExistValidWhitelist error:" + e.getMessage(), e);
            return false;
        }
    }

}
